package fa.wangxing.leaf.Controller;


import fa.wangxing.leaf.Service.Cartservice;
import fa.wangxing.leaf.pojo.Cart;
import fa.wangxing.leaf.pojo.User;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {
    @Resource
    private Cartservice cartservice;

    @GetMapping("/addtocart/{id}")
    public String addtocart(@PathVariable int id, HttpSession session,ModelMap m) {
        if(session.getAttribute("session_user") == null){
            m.put("msg","未登录，请登录");
            return "redirect:/user/tologin";
        }
        System.out.println(id);
        User user = (User) session.getAttribute("session_user");
        System.out.println(user);
        cartservice.addtocart(user.getId(),id);
        return "redirect:/dishes/getdishes";
    }

    @RequestMapping("/showcart")
    public String showcart(HttpSession session, ModelMap m) {
        if(session.getAttribute("session_user") == null){
            m.put("msg","未登录，请登录");
            return "redirect:/user/tologin";
        }
        User user = (User) session.getAttribute("session_user");
        List<Cart> cartlist = cartservice.getcart(user.getId());
        m.put("cartlist",cartlist);
        return "showcart";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable int id, HttpSession session,ModelMap m) {
        cartservice.deletebyId(id);
        return this.showcart(session, m);
    }
}
